/**
 * 冷却塔历史记录
 */
'use strict';

const conn = require('../../libs/DB/Conn');
const _ = require("underscore");
const {timeComponent,chartDataProcessing} = require("../../libs/Utils");
const columnNameObj = require("./deviceChartConfig").chart.coolingTower;
const deviceType = require("./deviceChartConfig").type.coolingTower;
const moment = require("moment");

module.exports = async function (params) {
    try{
    let timeInterval = timeComponent(params.startTime,params.endTime,params.type);
    let sql = `SELECT
        dd.device_no AS deviceNo,
        DATE_FORMAT(dd.data_time,"%Y-%m-%d %H:%i:%s") AS dataTime,
        dd.runtime,
        ${timeInterval.queryGroupTime} 
        df.device_name AS deviceName,
        dd.runtime,
        dd.status,
        dd.fault_type AS faultType,
        dd.cooling_water_supply_temperature AS coolingWaterSupplyTemperature,
        dd.cooling_water_return_temperature AS coolingWaterReturnTemperature,
        dd.cooling_water_supply_return_pressure_delta AS coolingWaterSupplyReturnPressureDelta,
        dd.outdoor_temperature AS outdoorTemperature,
        dd.outdoor_humidity AS outdoorHumidity,
        dd.wet_bulb_temperature AS wetBulbTemperature,
        rf.range_name AS rangeName
    FROM
    data_cooling_tower_${params.userDomainId} dd
    INNER JOIN device_file df ON df.device_no = dd.device_no
    AND df.delete_sign = 1 AND df.device_type = ${deviceType} 
    AND df.range_id IN (?)
    INNER JOIN range_file rf ON df.range_id = rf.range_id AND rf.delete_sign = 1 
    WHERE 1 = 1 ${timeInterval.startTime && timeInterval.endTime ? ` AND (dd.data_time > "${timeInterval.startTime}" AND dd.data_time <= "${timeInterval.endTime}")` : `` } 
    ORDER BY dd.data_time DESC`;
    let heatCoolingDataList = await conn(sql,[params.rangeIds]);
    let heatDataGroup = _.groupBy(heatCoolingDataList,"deviceNo");
    let dataList = [];
    for(const deviceNo in heatDataGroup){
        let deviceDataList = heatDataGroup[deviceNo];
        let deviceObj = {
            deviceName:deviceDataList[0].deviceName,
            rangeName:deviceDataList[0].rangeName,
            deviceNo:deviceNo,
            runtime:"00:00:00",
            status:deviceDataList[0].status,
            faultType:deviceDataList[0].faultType,
            coolingWaterSupplyTemperature:deviceDataList[0].coolingWaterSupplyTemperature,
            coolingWaterReturnTemperature:deviceDataList[0].coolingWaterReturnTemperature,
            coolingWaterSupplyReturnPressureDelta:deviceDataList[0].coolingWaterSupplyReturnPressureDelta,
            outdoorTemperature:deviceDataList[0].outdoorTemperature,
            outdoorHumidity:deviceDataList[0].outdoorHumidity,
            wetBulbTemperature:deviceDataList[0].wetBulbTemperature,
            dataList:[],
        }
        deviceDataList.forEach(function (data){
            let runtime = moment.duration(deviceDataList[0].runtime).add(moment.duration(data.runtime));
            deviceObj.runtime =  moment.utc(runtime.asMilliseconds()).format('HH:mm:ss');
        })
        if(params.type === "hour"){
            deviceObj.dataList = deviceDataList
        }else {
            let timeGroup = _.groupBy(deviceDataList,"groupTime");
            for(const time in timeGroup){
                let timeGroupList = timeGroup[time];
                let deviceDataObj = {
                    dataTime:time,
                    dataCreateTime:timeGroupList[timeGroupList.length - 1].dataTime,
                    runtime:"00:00:00",
                    status:timeGroupList[0].status,
                    faultType:timeGroupList[0].faultType,
                    coolingWaterSupplyTemperature:timeGroupList[0].coolingWaterSupplyTemperature,
                    coolingWaterReturnTemperature:timeGroupList[0].coolingWaterReturnTemperature,
                    coolingWaterSupplyReturnPressureDelta:timeGroupList[0].coolingWaterSupplyReturnPressureDelta,
                    outdoorTemperature:timeGroupList[0].outdoorTemperature,
                    outdoorHumidity:timeGroupList[0].outdoorHumidity,
                    wetBulbTemperature:timeGroupList[0].wetBulbTemperature,
                }
                deviceDataList.forEach(function (data){
                    let runtime = moment.duration(deviceDataObj.runtime).add(moment.duration(data.runtime));
                    deviceDataObj.runtime =  moment.utc(runtime.asMilliseconds()).format('HH:mm:ss');
                })
                deviceObj.dataList.push(deviceDataObj);
            }
        }
        deviceObj.chart = chartDataProcessing(deviceObj.dataList,columnNameObj);
        dataList.push(deviceObj);
    }
    return { success: true,dataList:dataList};
}catch (e){
    return { success: false,dataList:[],message:e.code === "ER_NO_SUCH_TABLE" ? "当前账号无此类设备" : e.sqlMessage};
}
};